<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ZUser {

    public $conn;

    public function __construct() {
        $this->conn = Yii::app()->db;
    }

    /**
     * 得到当前角色用户
     * 
     * @param $role_id
     * 
     * @version 1.0
     * @date 2013-12-12
     * @author jinyue.wang
     */
    public function _getRoleUser() {
        
    }

    /**
     * 得到用户基本详细信息
     * 
     * @param $uid
     * @return array
     * 
     * @author jinyue.wang 
     * @date 2013-12-13
     * @version 1.0
     */
    public function _getUserInfomation($uid) {

        if (empty($uid))
            return null;
        
        $sqlSelect = 'SELECT A."id",a."username",A."role_id",A."lastlogin_ip",A."lastlogin_time",A."email",A."realname",A."status",A."comment",B."school_id",B."subject_id" FROM "{{user}}" A LEFT JOIN "{{user_school}}" B ';
        $sqlSelect .= ' ON A."id" = B."user_id" ';
        $sqlSelect .= ' WHERE A."id" = :id_s ';
        
        $result = $this->conn->createCommand($sqlSelect);
        $result->bindParam(':id_s',$uid,PDO::PARAM_STR);
        
        $data = $result->queryRow();
        
        return $data;
    }
    
    /**
     * 得到当前用户的基本信息
     * 
     * @param $uid
     * 
     * @return array
     * 
     * @author jinyue.wang
     * @date 2013-12-12
     * @version 1.0
     */
    public function _getUserInfo($uid){
        if(empty($uid))
            return null; 
        $sql = 'SELECT "username","email","role_id","realname","salt","status" FROM "{{user}}" WHERE "id" = :userid ';
        $result = $this->conn->createCommand($sql);
        $result->bindParam(":userid", $uid);
        
        $row = $result->queryRow();
        
        return $row;
    }
    
    /**
     * 得到当前用户对应的学校
     * 
     * @param $uid
     * 
     * @return array()
     * 
     * @author jinyue.wang
     * @version 1.0
     * @date 2013-12-13
     */
    public function _getUserSchool($uid){
        
        if(empty($uid))
            return null;
        
        $sql = 'SELECT "user_id","school_id","subject_id" FROM "{{user_school}}" WHERE "user_id" = :uid_s ';
        $result = $this->conn->createCommand($sql);
        $result->bindParam(':uid_s',$uid,PDO::PARAM_STR);
        
        $data = $result->queryRow();
        
        return $data;
    }
    
    /**
     * 得到所有学校信息
     * 
     * @param $school_id
     * 
     * @return array
     * 
     * @author jinyue.wang
     * @version 1.0
     * @date 2013-12-13
     */
    public function _getSchoolInfo($school_id){
        
        if(empty($school_id))
            return null;
        
        $sql = 'SELECT "school_id","school_name","school_nick","school_code","edu_type_ids" FROM "{{school}}" WHERE "school_id" = :school_id ';
        $result = $this->conn->createCommand($sql);
        $result->bindParam(':school_id',$school_id,PDO::PARAM_STR);
        
        $data = $result->queryRow();
        
        return $data;
    }
    
    /**
     * 得到学校列表 
     * 
     * @author jinyue.wang
     * @version 1.0
     * @date 2013-12-13
     */
    public function _getSchoolList(){
        
        $sql = 'SELECT "school_id","school_name","school_nick","school_code","edu_type_ids" FROM "{{school}}" ';
        $result = $this->conn->createCommand($sql);
        
        $data = $result->queryAll();
        
        return $data;
    }
    
    /**
     * 得到学校列表信息
     * 
     * @version 1.0
     * @author jinyue.wang
     * @date 2013-12-13
     */
    public function _getSchoolObject(){
        
        $new_data = array();
        
        $sql = 'SELECT "school_id","school_name","school_nick","school_code","edu_type_ids" FROM "{{school}}" WHERE "is_del"=0 AND "disabled"=1  ';
        $result = $this->conn->createCommand($sql);
        
        $data = $result->queryAll();
        if(!empty($data)){
            foreach($data as $key => $value){
                
                $new_data[$value['school_id']] = $value;
            }
            
        }
        
        return $new_data;
    }
    
    /**
     * 更新用户信息
     * 
     * @param $uid
     * @param $data
     * 
     * @return boolean
     * 
     * @version 1.0
     * @date 2013-12-13
     * @author jinyue.wang
     */
    public function _updUserInfo($uid,$data){
        
        $status = 0;
        if ($uid > 0) {
            //判断当前用户是否存在
            $userInfo = self::_getUserInfo($uid);
            if (!empty($userInfo)) {
                
                $sta = 0;
                if(isset($data['status'])){
                    $sta = 1;
                }
                $setList = '';
                if(!empty($data['password'])){
                    $m = md5(md5($data["password"]).$userInfo['salt']);
                    $setList .= ',"password" = \''.md5(md5($data["password"]).$userInfo['salt']).'\'';
                }
                
                //保存用户信息
                $sql = 'UPDATE "{{user}}" SET "username" = :username_s,"role_id" = :role_id_s,"realname" = :real_name_s,"comment" = :comment_s,"status" = :status_s '.$setList.' WHERE "id" = :user_id ';
                $result = $this->conn->createCommand($sql);
                
                $result->bindParam(':user_id',$uid,PDO::PARAM_STR);
                $result->bindParam(':username_s', $data['username'], PDO::PARAM_STR);
                $result->bindParam(':role_id_s', $data['role'], PDO::PARAM_STR);
                $result->bindParam(':real_name_s', $data['username'], PDO::PARAM_STR);
                $result->bindParam(':comment_s', $data['comment'], PDO::PARAM_STR);
                $result->bindParam(':status_s', $sta, PDO::PARAM_STR);
                
                $status = $result->execute();
                $schoolUser = self::_getUserSchool($uid);
                $type = 0;
                if (!empty($schoolUser))
                    $type = 1;

                if ((isset($data['school']) && !empty($data['school'])) || (isset($data['subject']) && !empty($data['subject']))) {
                    if (!isset($data['school'])) {
                        $data['school'] = null;
                    }
                    if (!isset($data['subject'])) {
                        $data['subject'] = null;
                    }
                    self::_addSchoolUser($uid, $data['school'], $data['subject'], $type);
                } else {
                    self::_delSchoolUser($uid); 
                }
                
            }
        } else {

            $ip = Func::get_ip();
            $salt=  Func::genSalt(6);
            $password = md5(md5($data['password']).$salt);
            $time = time();
            
            $sta = 0;
            if(isset($data['status'])){
                $sta = 1;
            }
            
            $sql = 'INSERT INTO "{{user}}" ("username","password","role_id","salt","lastlogin_ip","lastlogin_time","realname","comment","status") ';
            $sql .= 'VALUES (:username_s,:pass_s,:role_s,:salt_s,:ip_s,:time_s,:rname_s,:comment_s,:status_s) ';
            $result = $this->conn->createCommand($sql);
            
            $result->bindParam(':username_s',$data['username'],PDO::PARAM_STR);
            $result->bindParam(':pass_s',$password,PDO::PARAM_STR);
            $result->bindParam(':role_s',$data['role'],PDO::PARAM_STR);
            $result->bindParam(':salt_s',$salt,PDO::PARAM_STR);
            $result->bindParam(':ip_s',$ip,PDO::PARAM_STR);
            $result->bindParam(':time_s',$time,PDO::PARAM_STR);
            $result->bindParam(':rname_s',$data['username'],PDO::PARAM_STR);
            $result->bindParam(':comment_s',$data['comment'],PDO::PARAM_STR);
            $result->bindParam(':status_s',$sta,PDO::PARAM_STR);
            
            $status = $result->execute();
            if ($status) {

               // if (!empty($data['school']) && !empty($data['subject'])) {
                    //$user_id = Yii::app()->db->getLastInsertID();
                    $sql = 'SELECT "id" FROM "{{user}}" WHERE "username" = :username_s AND "role_id" = :role_ids ';
                    $res = $this->conn->createCommand($sql);
                    $res->bindParam(':username_s', $data['username'], PDO::PARAM_STR);
                    $res->bindParam(':role_ids', $data['role'], PDO::PARAM_STR);
                    $row = $res->queryRow();
                    if (!empty($row)) {
                        $user_id = $row['id'];
                        if (!isset($data['school'])) {
                            $data['school'] = null;
                        }
                        if (!isset($data['subject'])) {
                            $data['subject'] = null;
                        }
                        self::_addSchoolUser($user_id, $data['school'], $data['subject']);
                    }
               // }
            }
        }
        
        return $status;
    }
    
    /**
     * 保存用户学校关系
     * 
     * @param $uid
     * @param $school_id
     * @param $subject_id
     * 
     * @return boolean
     * 
     * @version 1.0
     * @author jinyue.wang
     * @date 2013-12-13
     */
    public function _addSchoolUser($user_id, $school_id, $subject_id,$type=0) {

        $status = 0;

        if ($school_id > 0 || $subject_id > 0) {
            if ($school_id != null) {
                $school_id = implode(',', $school_id);
            }
            if ($subject_id != null) {
                $subject_id = implode(',', $subject_id);
            }
            if ($type == 0) {
                $isql = 'INSERT INTO "{{user_school}}" ("user_id","school_id","subject_id") VALUES (:user_ids,:school_ids,:subject_ids) ';
                $iresult = $this->conn->createCommand($isql);

                $iresult->bindParam(':user_ids', $user_id, PDO::PARAM_STR);
                $iresult->bindParam(':school_ids', $school_id, PDO::PARAM_STR);
                $iresult->bindParam(':subject_ids', $subject_id, PDO::PARAM_STR);

                $status = $iresult->execute();
            }else{
                $isql = 'UPDATE "{{user_school}}" SET "school_id" = :school_id,"subject_id" = :subject_id WHERE "user_id" = :user_id ';
                $iresult = $this->conn->createCommand($isql);

                $iresult->bindParam(':user_id', $user_id, PDO::PARAM_STR);
                $iresult->bindParam(':school_id', $school_id, PDO::PARAM_STR);
                $iresult->bindParam(':subject_id', $subject_id, PDO::PARAM_STR);

                $status = $iresult->execute();
            }
        }
        
        return $status;
    }

    /**
     * 清除用户学校关系
     * 
     * @param $uid
     * @param $school_id
     * @param $subject_id
     * 
     * @return boolean
     * 
     * @version 1.0
     * @author wf
     * @date 2013-12-13
     */
    public function _delSchoolUser($user_id) {
        $status = 0;
        $isql = ' DELETE  FROM "{{user_school}}" WHERE "user_id" = :user_id ';
        $iresult = $this->conn->createCommand($isql);
        $iresult->bindParam(':user_id', $user_id, PDO::PARAM_STR);
        $status = $iresult->execute();
        return $status;
    }

}

?>
